/**
 * 图片地址
 */
export const IMG_BASE = 'https://www.bigzhuan.com/assets/'

/**
 * 获取style样式
 */
export const getStyle = (element, attr, NumberMode = 'int') => {
    let target;
    // scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
    if (attr === 'scrollTop') { 
        target = element.scrollTop;
    }else if(element.currentStyle){
        target = element.currentStyle[attr]; 
    }else{ 
        target = document.defaultView.getComputedStyle(element,null)[attr]; 
    }
    //在获取 opactiy 时需要获取小数 parseFloat
    return  NumberMode == 'float'? parseFloat(target) : parseInt(target);
} 

/**
 * 判断input值是否符合要求
 * value:input值
 * type:input类型;
 *      1：手机号
 *      2：密码
 *      3：验证码
 *      4: 姓名
 *      5：身份证
 * 返回值：
 *      
 */
export const verify = (value,type) => {
    switch(type){
        case 1:
            let partten = /^1\d{10}$/;//手机号格式
            if(partten.test(value)){
               return true;
            }
        break
        case 2:
            let patrn1 = /^(\w){6,16}$/; //密码格式
            if(patrn1.test(value)){
                return true;
            }
        break
        case 3:
            let patrn2 = /\d{4}/; //验证码格式
            if(patrn2.test(value)){
                return true;
            }
        break
        case 4:
            let patrn3 = /^[\u4e00-\u9fa5]{2,4}$/; //姓名格式
            if(patrn3.test(value)){
                return true;
            }
        break
        case 5:
            let patrn4 = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //身份证格式
            if(patrn4.test(value)){
                return true;
            }
        break
    }
}

/**
 * 存储sessionStorage
 */
export const setStore = (name, content) => {
	if (!name) return;
	if (typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	window.sessionStorage.setItem(name, content);
}

/**
 * 获取sessionStorage
 */
export const getStore = name => {
	if (!name) return;
	return window.sessionStorage.getItem(name);
}

/**
 * 删除sessionStorage
 */
export const removeStore = name => {
	if (!name) return;
	window.sessionStorage.removeItem(name);
}

/**
 * 存储localStorage
 */
export const setLStore = (name, content) => {
	if (!name) return;
	if (typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getLStore = name => {
	if (!name) return;
	return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeLStore = name => {
	if (!name) return;
	window.localStorage.removeItem(name);
}

/**
 * 提示信息
 */
export const showMsg = (txt,context) => {
    context.msg = txt;
    context.showPositionValue = true;
}

/**
 * 获取url参数值
 */
export function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    var q = window.location.pathname.substr(1).match(reg_rewrite);
    if(r != null){
        return unescape(r[2]);
    }else if(q != null){
        return unescape(q[2]);
    }else{
        return null;
    }
}

export function getQS(name){
    let hashUrl = location.hash
    if(hashUrl.match(/h=(\d+).*/)){
        return location.hash.replace(/.*h=(\d+).*/,'$1')
    }
}